<template>
    <div class="box">
        <div class="right-box">
            <div class="title">
                <div class="title-left">注册</div>
                <div class="title-right">
                    <span>已有账号?</span>
                    <a href="login">前往登录</a>
                </div>
            </div>
            <div class="hezi1">
                <p class="p1">手机号</p>
                <input class="username" type="text"  v-model="user.phone" placeholder="请输入账号">
                <p class="hidden" v-if="userState.user1">账号不能为空</p>
                <p class="hidden" v-if="userState.user2">格式错误，请重新输入</p>
            </div>
            <div class="hezi3">
                <p class="p1">验证码</p>
                <div class="yanBox">
                    <input class="Yan" type="text"  v-model="user.code">
                    <button id="btn2" @click="getCode" v-if="countdown == 0" >发送验证码</button>
                    <button id="btn3" :disabled="true"  @click="getCode" v-if="countdown != 0">{{countdown}}s</button>
                </div>
                <p class="hidden" v-if="userState.yan1">验证码不能为空</p>
                <p class="hidden" v-if="userState.yan2">验证码输入错误，请重新输入</p>
            </div>

            <button id="btn1" @click="registerUser">注册</button>


        </div>
    </div>
</template>

<script setup >

import {getCodeApi, registerUserApi} from "@/api/user.js"
import { reactive, ref, onMounted, computed } from "vue";

import Message from "@/pulging/Message/js/Message.js"

import { useRouter} from "vue-router"


const router =  useRouter()

const reg = /^1([3-9]{2}) \d $/
const reg1 = /[0-9] \d $/
const user = reactive(
    {
    phone: 19924905300,
    code:  "",
    password:"",
}
)
let countdown = ref(0)

const userState = ref({
    user1: false,
    user2: false,
    yan1:false,
    yan2:false,
    password: false,
});
function clickUser() {
    if (user.username.length == 0) {
    userState.value.user1 = true;
    userState.value.user2 = false;
  }else if(!reg.test(user.username)){
    userState.value.user1 = false;
    userState.value.user2 = true;
  } else {
    userState.value.user1 = false;
    userState.value.user2 = false;
  }
}
function clickYan(){
    if (user.yan.length == 0) {
    userState.value.yan1 = true;
    userState.value.yan2 = false;
  }else if(!reg1.test(user.yan)){
    userState.value.yan1 = false;
    userState.value.yan2 = true;
  } else {
    userState.value.yan1 = false;
    userState.value.yan2 = false;
  }
}

function clickPassword() {
    if (user.password == "") {
        userState.value.password = true;
    } else {
        userState.value.password = false;
    }
}

const getCode = async() => {
    
    let nowTime = new Date().getTime()/1000
    let endTime = nowTime + 60
    countdown.value = 60
   let res  = await getCodeApi(user.phone)
   if (res.status == 200) {
        Message({type: "success", text: "验证码发送成功"})
   } else {
    Message({type: "warn", text: "发送失败请重新发送"})
    return
   }

  let timer  = setInterval(() => {
        if (endTime >= new Date().getTime()/1000) {
            countdown.value -=1
        } else {
            countdown.value = 0
            console.log(endTime, new Date().getTime()/1000);
            clearInterval(timer)
        }
   }, 1000)
}

const registerUser = async() => {
    let {phone:phoneNum, code} = user
       let code1 = Number(code)
        let res = await registerUserApi({phoneNum, code:code1 })
        if (res.status == 200) {
            router.push("/login")
        }
        router.push({path: "/login", query: { phone: user.phone}})
}
</script>

<style scoped lang=scss>
.box {
    width: 100vw;
    height: 100vh;
    background-image: url("../../assets/imgs/编组3.png");
    background-size: 100%;
    .right-box {
        width: 5rem;
        position: absolute;
        top: 3.5rem;
        right: 2.58rem;
        .hidden {
            // display: none;
            color: red;
        }
        .title {
            width: 4.95rem;
            height: 0.67rem;
            // background-color: pink;
            margin-bottom: 0.55rem;
            display: flex;
            justify-content: space-between;
            .title-left {
                font-size: 0.6rem;
                color: black;
                font-weight: 600;
            }
            .title-right {
                line-height: 1.27rem;
                font-size: 0.2rem;

                span {
                    color: #4D4D4D;
                }

                a {
                    color: #139DFF;
                }
            }
        }
        .hezi1,
        .hezi2 {
            height: 0.84rem;
            margin-bottom: 0.85rem;

            .p1 {
                margin-bottom: 0.13rem;
                color: #999999;
                font-size: 0.3rem;
            }
            input {
                width: 5rem;
                height: 0.77rem;
                outline: none;
                color: #999999;
               
                background-color: white;
                border: 0.01rem solid #d8d8d8;
                padding-left: 0.17rem;
                border-radius: 0.06rem;
            }
            .hidden {
                font-size: 0.17rem;
                color: red;
            }
        }
        .hezi3 {
            height: 0.84rem;
            margin-bottom: 0.85rem;
            .p1 {
                margin-bottom: 0.13rem;
                color: #999999;
                font-size: 0.3rem;
            }
            .yanBox {
                width: 5rem;
                height: 0.76rem;
                
                border-radius: 0.06rem;

                input {
                    width: 3.25rem;
                    height: 0.76rem;
                    outline: none;
                    color: #999999;
                    box-sizing: border-box;
                    border: 0.01rem solid #d8d8d8;
                    padding-left: 0.17rem;
                    border-radius: 0.06rem;
                }
                #btn2 {
                    width: 1.7rem;
                    height: 100%;
                    border: none;
                    outline: none;
                    background-color: #139DFF;
                    color: white;
                    border-radius: 0.06rem;
                }
                #btn3  {
                    width: 1.7rem;
                    height: 100%;
                    border: none;
                    outline: none;
                    background-color: #cccc;
                    color: white;
                    border-radius: 0.06rem;
                    cursor: not-allowed;
                }
            }
            .hidden {
                font-size: 0.17rem;
                color: red;
            }
        }
        #btn1 {
            width: 5rem;
            font-size: 0.3rem;
            color: white;
            height: 0.77rem;
            // margin-top: 0.25rem;
            margin-bottom: 0.31rem;
            outline: none;
            background-color: #139DFF;
            border-radius: 0.06rem;
            border: 0.01rem solid #d3d3d3;
        }
    }
}
</style>